package com.higgins.lawapp.client.maintenance.components;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.higgins.lawapp.client.maintenance.model.DataItemModel;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemContainerDTO;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemInstanceDTO;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemLevelDTO;

public class DataItemInstanceListBox extends LayoutPanel implements ChangeHandler 	{
	private DataItemModel dataItemModel;
	private DataSourceInstanceDeleteButton deleteButton;
	private ListBox listBox = new ListBox();
	private DataItemTable dataItemTable;
	private DataItemLevelDTO dataItemLevelDTOSelected;
	private List<String> fieldNames ;
	
	public DataItemInstanceListBox(DataItemModel dataItemModel, DataItemTable dataItemTable)	{
		deleteButton = new DataSourceInstanceDeleteButton(dataItemModel);
		this.dataItemModel = dataItemModel;
		this.dataItemTable = dataItemTable;
		listBox.addItem("None");
		listBox.setSelectedIndex(0);
		listBox.addChangeHandler(this);
		setWidth("800px");
		setHeight("25px");
		deleteButton.setTitle("Delete Instance");
		deleteButton.setText("Delete Instance");
		add(listBox);
		add(deleteButton);
	    setWidgetLeftWidth(listBox, 0, Unit.PCT, 80, Unit.PCT);
	    setWidgetRightWidth(deleteButton, 0, Unit.PCT, 20, Unit.PCT);
	}
	
	public void setDataItemLevelDTOSelected(DataItemLevelDTO dataItemLevelDTOSelected)	{
		this.dataItemLevelDTOSelected = dataItemLevelDTOSelected;

		clearList();
		addDataItemInstanceNamesToList(dataItemLevelDTOSelected);
		onChange(null);
	}
	
	private void addDataItemInstanceNamesToList(DataItemLevelDTO dataItemLevelDTOSelected)	{
		if (dataItemLevelDTOSelected!=null)	{
			List<DataItemInstanceDTO> dataItemInstanceDTOs = dataItemLevelDTOSelected.getDataItemInstanceDTOs();
			if (dataItemInstanceDTOs!=null)	{
				Iterator<DataItemInstanceDTO> dataItemInstanceIterator = dataItemInstanceDTOs.iterator();
				while (dataItemInstanceIterator.hasNext())	{
					DataItemInstanceDTO dataItemInstanceDTO = dataItemInstanceIterator.next();
					addItem(dataItemInstanceDTO.getDataItemInstance());
				}
			}
		}
	}
	
	@Override
	public void onChange(ChangeEvent event) {
		dataItemModel.setDataItemInstanceSelected(getDataItemInstanceDTO());
		dataItemTable.setDataItemInstanceSelected(getDataItemInstanceDTO());
	}
	
	public DataItemInstanceDTO getDataItemInstanceDTO()	{
		DataItemInstanceDTO dataItemInstanceDTO = null;
		if (listBox.getSelectedIndex() > 0)	{
			int selectedIndex = listBox.getSelectedIndex() - 1;
			List<DataItemInstanceDTO> dataItemInstanceDTOs = dataItemLevelDTOSelected.getDataItemInstanceDTOs();
			dataItemInstanceDTO = dataItemInstanceDTOs.get(selectedIndex);
		}
		return dataItemInstanceDTO;
	}
	
	public void addItem(String item)	{
		listBox.addItem(item);
	}
	public void setSelectedIndex(int index)	{
		listBox.setSelectedIndex(index);
	}
	public void clearList()	{
		listBox.clear();
		listBox.addItem("None");
		listBox.setSelectedIndex(0);
	}
}
